$(document).ready(function() {
    
     $("#form_curso").validate({
        //submitHandler: function(form) {
        //    $("#form_curso").submit();
        //},
        onfocusout: function (element) {
            $(element).valid();
        },
        errorPlacement: function(error, element) 
        {
            element.attr('title', error.text());
            $(".error").tooltip({   
                position: {
                    my: "center bottom-10",
                    at: "right top",
                    using: function(position, feedback) {
                        $(this).css(position);
                        $("<div>").addClass("arrow").addClass(feedback.vertical).addClass(feedback.horizontal).appendTo(this);
                    }
                },
                tooltipClass: "ttError"
            }); 
        }
    });
    
    $(".chosen-select").chosen();
    
    if($('.tpotmp:checked').val() == 1 || $('.tpotmp:checked').val() == undefined) $('#CUR_SEM').css('display','none');
    $('.tpotmp').click(function(){
        if($(this).val() == 2) $('#CUR_SEM').css('display','block');
        else $('#CUR_SEM').css('display','none');
    });
    
    $('#CBS_ID,.CUR_SEM').change(function(){
        modal();
        $.post(URLBASE+'/'+CONTROL+'/buscacursobase',{ cbs_id: $('#CBS_ID').val(), cur_id: $('#CUR_ID').val()
            , cur_sem : $('.CUR_SEM:checked').val(), cur_ano: $('#CUR_ANO').val() }, function(data) {
            if(data.ok){
                //$(".tur_id").attr("checked", false);
                //$('input:radio[name="TUR_ID"][value="'+data.TUR_ID+'"]').attr('checked',true);   
                $('#tipo_curso').html(data.tipo_curso);
                //$("#CUR_TRM").val(data.CUR_TRM).trigger("chosen:updated");
                if(data.CUR_TPOTMP == 2){
                    $('#CUR_SEM').css('display','block');
                    $('input:radio[name="CUR_SEM"][value="'+data.CUR_SEM+'"]').attr('checked',true);
                }
                else{
                    //$(".CUR_SEM").attr("checked", false);
                    $('#CUR_SEM').css('display','none');
                }
                $('#turma_ajx').html(data.turma);
            }
            remove_modal();
        }, 'json');
    });
    
    $('#CUR_ANO').blur(function(){ $('#CBS_ID').change(); });
    
});